package com.yida.gwj.common.service.impl;

import cn.hutool.core.date.DateUtil;
import com.yida.common.core.utils.StringUtils;
import com.yida.gwj.common.constant.GwjConstants;
import com.yida.gwj.common.domain.GwjBaseBusinessNum;
import com.yida.gwj.common.mapper.GwjBaseBusinessNumMapper;
import com.yida.gwj.common.service.IGwjBaseBusinessNumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务编号Service业务层处理
 *
 * @author lqq
 * @date 2022-11-14
 */
@Service
public class GwjBaseBusinessNumServiceImpl implements IGwjBaseBusinessNumService {

	@Autowired
	private GwjBaseBusinessNumMapper businessNumMapper;

	/**
	 * 获取下一个业务编号
	 *
	 * @param areaId    地区ID
	 * @param bizFlagId 业务标识ID
	 * @return 业务编号
	 */
	@Override
	public GwjBaseBusinessNum getNextNum(Long areaId, Integer bizFlagId) {
		int thisYear = DateUtil.thisYear();
		GwjBaseBusinessNum temp = new GwjBaseBusinessNum();
		temp.setYear(thisYear);
		temp.setAreaId(areaId);
		temp.setBizFlagId(bizFlagId);
		GwjBaseBusinessNum businessNum = businessNumMapper.selectGwjBaseBusinessNum(temp);
		//当年还未做业务
		if (StringUtils.isNull(businessNum)) {
			businessNum = new GwjBaseBusinessNum();
			businessNum.setYear(thisYear);
			businessNum.setAreaId(areaId);
			businessNum.setBizFlagId(bizFlagId);
			businessNum.setSeq(GwjConstants.BUSINESS_NUM_SEQ_FIRST);
			businessNumMapper.insertGwjBaseBusinessNum(businessNum);
		}
		//是当年并且已经开始业务，编号序列+1后便是新的业务编号
		else {
			businessNum.setSeq(businessNum.getSeq() + 1);
			businessNumMapper.updateGwjBaseBusinessNum(businessNum);
		}
		return businessNum;
	}
}
